<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        .layui-form-pane .layui-form-label {
            width: 120px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">工厂名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="supplierName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工厂编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="supplierNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工厂订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="contractNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-refresh-btn"><i class="layui-icon layui-icon-refresh"></i> 刷新
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                limit: 5,
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createSupplierContract">
                    生成工厂合同
                </button>
                <button id="sumButton" style="float: right; font-size: 16px"
                        class="layui-btn layui-btn-primary layui-btn-sm"><i
                        class="fa fa-arrows-h">工厂订单总金额：</i><span id="sumPrice"></span></li></button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-add" lay-event="show">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    var lay, form, table;
    layui.use(['form', 'table'], function () {
        lay = layui.jquery;
        form = layui.form;
        util = layui.util;
        table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '/supplierContract/pages',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: {
                param: {}
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, title: 'ID', sort: true},
                {field: 'contractNo', width: 120, title: '工厂订单编号'},
                {field: 'contractName', width: 120, title: '工厂订单名称'},
                {field: 'supplierNo', width: 120, title: '工厂编号'},
                {field: 'supplierName', width: 120, title: '工厂名称'},
                {field: 'supplierContractNo', width: 120, title: '工厂合同编号'},
                {
                    field: 'orderDate',
                    width: 100,
                    title: '下单日期',
                    templet: "<div>{{layui.util.toDateString(d.orderDate, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'supplierHandler', width: 120, title: '工厂经办人'},
                {field: 'handler', width: 120, title: '公司经办人'},
                {
                    field: 'contractStartTime',
                    width: 100,
                    title: '交货日期',
                    templet: "<div>{{layui.util.toDateString(d.customerStartTime, 'yyyy-MM-dd')}}</div>"
                },
                // {
                //     field: 'contractEndTime',
                //     width: 100,
                //     title: '合同结束时间',
                //     templet: "<div>{{layui.util.toDateString(d.customerEndTime, 'yyyy-MM-dd')}}</div>"
                // },
                {field: 'boxNumber', width: 140, title: '总箱量'},
                {field: 'totalItemNumber', width: 140, title: '数量'},
                {field: 'totalPrice', width: 140, title: '订单总金额'},
                {field: 'address', width: 200, title: '地址'},
                {field: 'contacts', width: 140, title: '联系人'},
                {field: 'phone', width: 150, title: '联系电话"'},
                {field: 'contractTerms', width: 200, title: '合同条款'},
                {field: 'remarks', width: 200, title: '备注要求明细'},
                {field: 'quotationNo', width: 120, title: '报价单编号'},
                {field: 'quotationName', width: 120, title: '报价单名称'},
                {
                    field: 'createTime',
                    width: 120,
                    title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'updateUser', width: 100, title: '修改人'},
                {
                    field: 'updateTime',
                    width: 120,
                    title: '修改时间',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}</div>"
                },
                {title: '操作', minWidth: 170, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
            ]],
            done: function (res, curr, count) {  //隐藏列
                if ($.common.userIsAdmin()) {
                    $("[data-field='updateUser']").css('display', 'none');
                    $("[data-field='updateTime']").css('display', 'none');
                }
                if ($.common.userIsShowPower()) {
                    let sumPrice = 0;
                    let data = res.data
                    if (data.length > 0) {
                        for (let i in data) {
                            if (data[i].totalPrice) {
                                sumPrice = sumPrice + data[i].totalPrice;
                            }
                        }
                    }
                    $("#sumPrice").html(sumPrice)
                } else {
                    $('#sumButton').css("display", "none");
                    $("[data-field='totalPrice']").css('display', 'none');
                }
            },
            limits: [5, 10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            search(data);
            return false;
        });

        form.on('submit(data-refresh-btn)', function (data) {
            // 提交刷新了当前页面
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加订单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/supplierContract/add.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'createSupplierContract') {  // 生成工厂合同

                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let id = checkStatus.data[0].id;
                let supplierContractNo = checkStatus.data[0].supplierContractNo;
                if (!supplierContractNo || '' == supplierContractNo) {
                    $.common.showErrorMessage("请完善工厂订单信息");
                    return false;
                }
                window.location.href = '/supplierContract/createSupplierContract/' + id;
                return false;
            }


        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'show') {
                var index = layer.open({
                    title: '查看订单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/supplierContract/add.html?supplierContractId=' + data.id
                        + "&quotationId=" + data.quotationId + "&supplierId=" + data.supplierId + "&isShow=true",
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            if ('customerStartTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            if ('customerEndTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            if ('orderDate' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改订单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/supplierContract/add.html?supplierContractId=' + data.id
                        + "&quotationId=" + data.quotationId + "&supplierId=" + data.supplierId,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            if ('customerStartTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            if ('customerEndTime' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            if ('orderDate' == key) {
                                let a = util.toDateString(data[key], 'yyyy-MM-dd HH:mm:ss')
                                body.find("#" + key).val(a);
                                continue;
                            }
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除？', function (index) {
                    let url = "/supplierContract/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

    });

    function search(data) {
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                param: data.field
            }
        }, 'data');
    }
</script>

</body>
</html>
